capture log close data
log using mml-data, name(data) replace text

//  program:    mml-data.do
//  task:		replication of Hall medical marijuana study
// 	input:		ndush2002.csv...ndsuh2008.csv
//	output:		mml-data.dta
//  project:    medical marijuana laws
//  author:     sam harper \ 01dec2011

//  #0
//  program setup

version 12
set linesize 80
clear all
macro drop _all


// #1
// bring in prevalence data from National Survey of Drug Use & Health

forvalues y=2002/2008{
	insheet stname pmua1 pmusea1 pmua2 pmusea2 pmua3 pmusea3 pmua4 pmusea4 ///
		prua1 prusea1 prua2 prusea2 prua3 prusea3 prua4 prusea4 ///
		using nsduh`y'.csv, clear
	
	drop if inlist(stname,"Total","Total U.S.","Northeast","Midwest", ///
		"South","West")

	forvalues i=1/4 {	
		foreach var of newlist pmuse pruse {
	
			gen `var'a`i'lp=strpos(`var'a`i',"(") // identify left bracket position
			gen `var'a`i'lb=substr(`var'a`i',`var'a`i'lp+1,4) // extract lower bound
			destring `var'a`i'lb, replace // convert to numeric
			drop `var'a`i'lp // drop bracket position var
			gen `var'a`i'up=strpos(`var'a`i',")") // identify right bracket position
			gen `var'a`i'ub=substr(`var'a`i',`var'a`i'up-5,4) // extract upper bound
			destring `var'a`i'ub, replace //convert to numeric
			drop `var'a`i'up // drop bracket position var
			rename `var'a`i' `var'a`i's // rename original var
			gen `var'a`i' = (`var'a`i'ub - `var'a`i'lb) / (2*1.96) // calc std err
			drop `var'a`i'ub `var'a`i'lb `var'a`i's // drop bounds and original var
			}
		}
	gen year=`y'
	save nsduh`y', replace
	}
use nsduh2002, clear
forvalues y=2003/2008 {
	append using nsduh`y'
	}

// #2
// create some labels, reshape the data to long

label var stname "state (string)"
encode stname, gen(state)
label var state "state (numeric)"

reshape long pmua prua pmusea prusea, i(state year) j(age)
label var year "year of data"
gen year0=year-2002
label var year0 "year (2002-3=0)"
label define year0 0 "2002-3" 1 "2003-4" 2 "2004-5" 3 "2005-6" 4 "2006-7" ///
	5 "2007-8" 6 "2008-9", modify
label values year0 year0

label var age "age group"
label define age 1 "12+y" 2 "12-17y" 3 "18-25y" 4 "26+y"
label values age age

renvars pmua prua pmusea prusea / pmu pru pmuse pruse
label var pmu "past month marijuana use prevalence"
label var pru "% reporting 'great risk' in monthly smoking marijuana"
label var pmuse "past month marijuana use prevalence SE"
label var pruse "% at great risk of smoking marijuana once a month SE"

// #3
// define passage of medical marijuana laws, as listed
// in Figure1 of Wall et al. Ann Epid 2011;21:714-6.

gen mmlf=0
label var mmlf "passed medical marijuana law (first yr)"
note mmlf: coding assumed by Hall et al.

* states with laws already in existence by 2002
replace mmlf=1 if inlist(stname,"California","Oregon","Washington","Alaska", ///
	"Maine","Colorado","Nevada","Hawaii") 

gen mmll=mmlf
label var mmll "passed medical marijuana law (last yr)"


* states passing laws since 2002
replace mmlf=1 if year>=2003 & inlist(stname,"Montana","Vermont") 
replace mmll=1 if year>=2004 & inlist(stname,"Montana","Vermont")

gen mml_1=(year==2002 & inlist(stname,"Montana","Vermont"))
gen mml0=(year==2003 & inlist(stname,"Montana","Vermont"))
gen mml1=(year==2004 & inlist(stname,"Montana","Vermont"))
gen mml2=(year>=2005 & inlist(stname,"Montana","Vermont"))

replace mmlf=1 if year>=2005 & stname=="Rhode Island"
replace mmll=1 if year>=2006 & stname=="Rhode Island"

replace mml_1=(year==2004 & stname=="Rhode Island")
replace mml0=(year==2005 & stname=="Rhode Island")
replace mml1=(year==2006 & stname=="Rhode Island")
replace mml2=(year>=2007 & stname=="Rhode Island")

replace mmlf=1 if year>=2006 & stname=="New Mexico"
replace mmll=1 if year>=2007 & stname=="New Mexico"

replace mml_1=(year==2005 & stname=="New Mexico")
replace mml0=(year==2006 & stname=="New Mexico")
replace mml1=(year==2007 & stname=="New Mexico")
replace mml2=(year>=2008 & stname=="New Mexico")

replace mmlf=1 if year>=2007 & stname=="Michigan" 
replace mmll=1 if year>=2008 & stname=="Michigan" 

replace mml_1=(year==2006 & stname=="Michigan")
replace mml0=(year==2007 & stname=="Michigan")
replace mml1=(year==2008 & stname=="Michigan")

replace mml2=(year>=2002 & inlist(stname,"Nevada","Hawaii"))

label var mml_1 "1 year prior to adoption"
label var mml0 "year of adoption"
label var mml1 "1 year after adoption"
label var mml2 "2 years after adoption"


* variable to define states that 'ever' pass a law

egen ever=total(mmlf), by(stname)
gen treated=0
replace treated=1 if ever>0
drop ever
label var treated "ever passed med. marijuana law"

* extended treatment variable, applied to states passing
* laws after last year of observed data (2009) NJ/AZ (2010) DE (2011)

gen treatex=treated
replace treatex=1 if inlist(stname,"New Jersey","Arizona","Delaware")
label var treatex "treated + states passing MML after 2008"

* state groupings defined by Wall et al.
gen mmlwall=0
label var mmlwall "time-constant state categories in Wall et al."

replace mmlwall=1 if inlist(stname,"California","Oregon","Washington","Alaska", ///
	"Maine","Colorado","Nevada","Hawaii")
	 
replace mmlwall=2 if inlist(stname,"Montana","Vermont","Rhode Island", ///
	"New Mexico","Michigan","New Jersey","Arizona","Delaware")

label define mmlwall 0 "Never MML" 1 "Already MML" 2 "Passed MML"
label values mmlwall mmlwall

gen wall=0
replace wall=1 if inlist(stname,"Montana","Vermont","Rhode Island", ///
	"New Mexico","Michigan","New Jersey","Arizona","Delaware")

replace wall=2 if inlist(stname,"California","Oregon","Washington","Alaska", ///
	"Maine","Colorado","Nevada","Hawaii")

replace wall=2 if mmlf==1
label define wall 0 "Never MML" 1 "MML-pre" 2 "MML-post"
label values wall wall
label var wall "time-varying state categories in Wall et al."

label define ny 0 no 1 yes
label values mmlf mmll treated treatex ny

compress
label data "2002-9 NSDUH state data"
note: State estimates of past month marijuana use and ///
perceived risk of use from National Survey on Drug Use ///
and Health. Downloaded from ///
www.oas.samhsa.gov/states.cfm on 13aug2011 \ mml-data.do sh 03oct2011
note: updated with 2008-9 data on 01dec2011 \ mml-data.do sh 01dec2011

datasignature set, reset
save mml-data.dta, replace

log close data
exit
